import tkinter as tk

def rearrange_pages(event=None):
    n = int(pages_input.get())
    pages = list(range(1, n+1))
    for i in range(0, n-3, 4):
        group = pages[i:i+4]
        group[0], group[1], group[2], group[3] = group[3], group[0], group[1], group[2]
        pages[i:i+4] = group
    if n % 4 != 0:
        last_group = pages[-(n % 4):]
        pages[-(n % 4):] = last_group[::-1]
    result_text.delete('1.0', tk.END)
    result_text.insert(tk.END, ', '.join(map(str, pages)))

def exit_program():
    root.destroy()

root = tk.Tk()
root.title("Перестановка страниц")

pages_label = tk.Label(root, text="Количество листов в документе:")
pages_label.pack()
pages_input = tk.Entry(root)
pages_input.pack()
pages_input.bind("<Return>", rearrange_pages)  # добавление обработчика

rearrange_button = tk.Button(root, text="Перестановка страниц", command=rearrange_pages)
rearrange_button.pack()

result_text = tk.Text(root)
result_text.pack(fill="both", expand=True)

exit_button = tk.Button(root, text="Закрыть программу", command=exit_program)
exit_button.pack()

root.mainloop()
